package com.wzd.taijiapp.util


import androidx.appcompat.app.AppCompatActivity
import java.util.*

class ActivityContainer private constructor() {

    fun addActivity(aty: AppCompatActivity) {
        activityStack.add(aty)
    }

    fun removeActivity(aty: AppCompatActivity) {
        activityStack.remove(aty)
    }


    /**
     * 结束所有的Activity
     */
    fun finishAllActivity() {
        var i = 0
        val size = activityStack.size
        while (i < size) {
            activityStack[i].finish()
            i++
        }
        activityStack.clear()
    }

    /**
     * 结束所有的Activity不包括自己
     */
    fun finishAllButNoMeActivity(aty: AppCompatActivity) {
        var i = 0
        val size = activityStack.size
        while (i < size) {
            if (aty !== activityStack[i]) {
                activityStack[i].finish()
            }
            i++
        }
    }

    /**
     * 有没有这个activity
     */
    fun haveThisActivity(aty: AppCompatActivity): Boolean {
        var i = 0
        val size = activityStack.size
        while (i < size) {
            if (aty === activityStack[i]) {
                return true
            }
            i++
        }
        return false
    }

    companion object {

        val instance = ActivityContainer()
        private val activityStack = LinkedList<AppCompatActivity>()
    }
}
